<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ajax网络请求</title>

</head>

<body>

    <button id="deletebtn">删除用户</button>
    <div></div>

    <script>
        const divEle = document.querySelector('div') // 显示服务器返回的数据
        const btnEle = document.querySelector('button')
        const deleteEle = document.querySelector('#deletebtn')
        deleteEle.addEventListener('click', function () {
            //1. 实例XMLHttpRequest对象
            let xhr = new window.XMLHttpRequest()
            //2.建立连接
            xhr.open('GET', 'http://10.7.176.81:8089/api/delete?uid=1')
            //3. 发送请求
            xhr.send()
            //4.处理响应数据
            xhr.onreadystatechange = function (e) {
                e = e || window.event
                target = e.target || e.srcElement
                if (target.readyState == 4) {
                    if (target.status == 200) {
                        let responseObj = JSON.parse(target.responseText) // 将响应的json字符串转为json对象
                        if (responseObj.resultCode == 1) {
                            alert('删除数据成功')
                        } else {
                            alert('删除失败')
                        }
                    }
                }
            }

        })
    </script>
</body>

</html>